package com.clickpaas.materialsprocurement.dao.Impl;

import com.bizcloud.ipaas.t0ba0ba6afb694026b6b4e4e048af2b43.d20210202135416.codegen.StdmaterielApi;
import com.bizcloud.ipaas.t0ba0ba6afb694026b6b4e4e048af2b43.d20210202135416.model.*;
import com.clickpaas.materialsprocurement.bean.MateralsDTOBean;
import com.clickpaas.materialsprocurement.config.AutoLoadConfig;
import com.clickpaas.materialsprocurement.constant.ConfConstant;
import com.clickpaas.materialsprocurement.constant.ConstantFile;
import com.clickpaas.materialsprocurement.dao.MaterialsDao;
import com.clickpaas.materialsprocurement.entity.MaterialsEntity;
import com.clickpaas.materialsprocurement.entity.PageInfoEntity;
import com.clickpaas.materialsprocurement.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Repository
public class MaterialsDaoImpl implements MaterialsDao {

    @Resource
    private MateralsDTOBean materalsDtoBean;
    @Resource
    private StdmaterielApi stdmaterielApi;

    /**
     * 根据条件查询物料数据
     *
     * @return List<PMSpurchaseApplyLineDTO1>
     */
    @Override
    public List<STDmaterielDTOResponse> queryMaterialsInfo(MaterialsEntity materialsEntity) {
        STDmaterielDTO stDmaterielDTO = materalsDtoBean.stDmaterielDTO();
        try {
            SpringUtil.copyPropertiesIgnoreNull(materialsEntity,stDmaterielDTO);
        }catch (Exception e){
            log.info("对象转换失败！转换的对象是：【{}】",materialsEntity);
            return null;
        }
        return queryMaterialsInfo(stDmaterielDTO);
    }

    /**
     * 分页查询全部物料数据
     *
     * @return List<PMSpurchaseApplyLineDTO1>
     */
    @Override
    public List<STDmaterielDTOPageInfoResponse> queryMaterialsInfo() {

        PageInfoEntity pageInfoEntity = new PageInfoEntity();
        pageInfoEntity.setPageNum(ConfConstant._PAGE_NUM);
        pageInfoEntity.setPageSize(ConfConstant._PAGE_SIZE);
        /**
         * 分页查询
         */
        PageInfoEntity tagetPageInfoEntity = getTotal(pageInfoEntity);
        if (null != tagetPageInfoEntity) {
            List<STDmaterielDTOPageInfoResponse> listAll = new ArrayList<>();
            for (int i = 1; i <= tagetPageInfoEntity.getRound(); i++) {
                DataResponseSTDmaterielDTOPageInfodata MateriasInfodata = queryMateriasByPageInfo(i, tagetPageInfoEntity.getPageSize());
                if (null != MateriasInfodata && MateriasInfodata.getDataList().size()>0){
                    listAll.addAll(MateriasInfodata.getDataList());
                }
            }
            if (listAll.size() > 0) {
                return listAll;
            }
        }
        return null;
    }

    /**
     * 物料数据入库
     *
     * @param materialsEntity
     * @return boolean
     */
    @Override
    public boolean insertMaterialsInfo(MaterialsEntity materialsEntity) {
        STDmaterielDTOUpdate stDmaterielDTOUpdate = materalsDtoBean.stDmaterielDTOUpdate();
        try {
            SpringUtil.copyPropertiesIgnoreNull(materialsEntity,stDmaterielDTOUpdate);
        }catch (Exception e){
            log.info("对象转换失败！转换的对象是：【{}】",materialsEntity);
        }
        SaveOrUpdateSTDmaterielDTO saveOrUpdateSTDmaterielDTO = new SaveOrUpdateSTDmaterielDTO();
        saveOrUpdateSTDmaterielDTO.setQuery(materalsDtoBean.stDmaterielDTOQuery());
        saveOrUpdateSTDmaterielDTO.setUpdate(stDmaterielDTOUpdate);
        return insertData(saveOrUpdateSTDmaterielDTO);
    }

    private Boolean insertData(SaveOrUpdateSTDmaterielDTO saveOrUpdateSTDmaterielDTO){
        try {
            STDmaterielSaveOrUpdateDataResponseObject dataResponseString = stdmaterielApi.saveOrUpdateSTDMaterielUsingPOST(saveOrUpdateSTDmaterielDTO);
            if (null != dataResponseString
                    && ConstantFile._MESSAGE.equals(dataResponseString.getMessage())
                    && null != dataResponseString.getData())
                return true;
        }catch (Exception e){
            log.info("物料数据入库失败：【{}】",e);
        }
        return false;
    }

    /**
     * 根据条件查询物料数据
     * @param stDmaterielDTO
     * @return
     */
    private List<STDmaterielDTOResponse> queryMaterialsInfo(STDmaterielDTO stDmaterielDTO){
        DataResponseListSTDmaterielDTO stdMaterielUsingPOST = stdmaterielApi.findSTDMaterielUsingPOST(stDmaterielDTO);
        if(null == stdMaterielUsingPOST
                || stdMaterielUsingPOST.getData().size()<=0
                || null == stdMaterielUsingPOST.getData()
                || !"SUCCESS".equals(stdMaterielUsingPOST.getMessage())){
            log.info("当前查询的数据：【{}】",stdMaterielUsingPOST);
            return null;
        }
        return stdMaterielUsingPOST.getData();
    }

    /**
     * 获取分页查询数据
     * @return
     */
    private PageInfoEntity getTotal(PageInfoEntity pageInfoEntity){
        Integer pageNum = pageInfoEntity.getPageNum();
        Integer pageSize = pageInfoEntity.getPageSize();
        DataResponseSTDmaterielDTOPageInfodata dataResponseSTDmaterielDTOPageInfodata = queryMateriasByPageInfo(pageNum, pageSize);
        if (null != dataResponseSTDmaterielDTOPageInfodata
                && StringUtils.isNotBlank(dataResponseSTDmaterielDTOPageInfodata.getTotal())){
            /**
             * 查询总数
             */
            String total = dataResponseSTDmaterielDTOPageInfodata.getTotal();
            double ceil = Math.ceil(Double.valueOf(total) / Double.valueOf(AutoLoadConfig.PAGE_SIZE));
            /**
             * 分页查询次数
             */
            Long round = Math.round(ceil);

            pageInfoEntity.setTotal(Integer.valueOf(total));
            pageInfoEntity.setPageSize(AutoLoadConfig.PAGE_SIZE);
            pageInfoEntity.setRound(round.intValue());

            return pageInfoEntity;
        }
        return null;
    }


    /**
     * 分页查询物料数据
     * @param pageNum
     * @param pageSize
     * @return
     */
    public DataResponseSTDmaterielDTOPageInfodata queryMateriasByPageInfo(Integer pageNum, Integer pageSize) {
        STDmaterielDTOPageInfo stDmaterielDTOPageInfo = materalsDtoBean.stDmaterielDTOPageInfo();
        stDmaterielDTOPageInfo.set$PageNum(pageNum);
        stDmaterielDTOPageInfo.set$PageSize(pageSize);
        DataResponseSTDmaterielDTOPageInfo pageInfoSTDMaterielUsingPOST = stdmaterielApi.findPageInfoSTDMaterielUsingPOST(stDmaterielDTOPageInfo);
        if (null == pageInfoSTDMaterielUsingPOST || null == pageInfoSTDMaterielUsingPOST.getData())return null;
        return pageInfoSTDMaterielUsingPOST.getData();
    }
}
